package yanglei.day0110;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: YangLei
 * @CreateTime: 2023-01-10  15:14
 * @Description: 自定义一个Servlet
 */
public class TestServlet extends HttpServlet {

    @Override
    public void init() {
        System.out.println("TestServlet 执行初始化");
    }

    @Override
    public void destroy() {
        System.out.println("TestServlet 执行销毁");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 执行get请求时，调用post请求方法
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置request、response响应，中文字符乱码情况
        request.setCharacterEncoding("UTF-8");
        //
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("执行doG请求et");
        // 创建JspWrite实例对象
        PrintWriter out = response.getWriter();
        out.write("<html>\r\n");
        out.write("<head>\r\n");
        out.write("    <title>初始Servlet</title>\r\n");
        out.write("    ");
        // 同一个请求内置对象
        // request.setAttribute("elNamePageRequest", name);
        out.write("\r\n");
        out.write("</head>\r\n");
        out.write("<body>\r\n");
        out.write("<h3 style='color:red;'>hello,Servlet!!!</h3>");
        out.write("<h3 style='color:red;'>你好,Servlet!!!</h3>");
        out.write("</body>\r\n");
        out.write("</html>\r\n");
    }
}
